package com.app.controller;

import com.app.model.*;
import com.app.service.AdminUserService;
import com.app.service.StatisticsService;
import com.app.util.*;
import com.app.vo.IndexCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Controller
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private AdminUserService adminUserService;

    @GetMapping("/getIndexCount")
    public Result<Object> getCusPlaceList(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {

        IndexCountVo vo = new IndexCountVo();
        if (StringTool.isNotEmpty(startTime) && StringTool.isNotEmpty(endTime))//带日期统计
        {
            int enterpriseCount = statisticsService.getCountByGroup(startTime, endTime, 1);//公司数
            int jobCount = statisticsService.sumJobNumberAll(startTime, endTime, 1);//职位数
            int workAppCount = statisticsService.getWorkAppCount(startTime, endTime, 1);//投递简历数
            int browseCount = statisticsService.getBrowseCount(startTime, endTime, 1);//网站浏览数
            int jobFairCount = statisticsService.getJobFairCount();//招聘会数
            int customerCount = statisticsService.getModelCountByTime(null, startTime, endTime, 1);//用户数
            int newsCount = statisticsService.getNewsCount();//政策数
            int comRecruitersCount = statisticsService.getComRecruitersCount();//招聘人员数
            int enterpriseWaitCount = statisticsService.getModelCount(2);//待审核企业数量
            int publishWaitCount = statisticsService.countWait();//未审核岗位数量
            int nosUserCount = statisticsService.getNosUserCount();//未审核招聘人员数

            vo.setEnterpriseCount(enterpriseCount);
            vo.setJobCount(jobCount);
            vo.setWorkAppCount(workAppCount);
            vo.setBrowseCount(browseCount);
            vo.setJobFairCount(jobFairCount);
            vo.setCustomerCount(customerCount);
            vo.setNewsCount(newsCount);
            vo.setPublishWaitCount(publishWaitCount);
            vo.setComRecruitersCount(comRecruitersCount);
            vo.setEnterpriseWaitCount(enterpriseWaitCount);
            vo.setNosUserCount(nosUserCount);
            return Result.success(vo);
        } else {
            int enterpriseCount = statisticsService.getCountByGroup(null, null, 2);//公司数
            int jobCount = statisticsService.sumJobNumberAll(null, null, 2);//职位数
            int workAppCount = statisticsService.getWorkAppCount(null, null, 2);//投递简历数
            int browseCount = statisticsService.getBrowseCount(null, null, 2);//网站浏览数
            int jobFairCount = statisticsService.getJobFairCount();//招聘会数
            int customerCount = statisticsService.getModelCountByTime("2023-07-14 00:00:00", null, null, 2);//用户数
            int newsCount = statisticsService.getNewsCount();//政策数
            int comRecruitersCount = statisticsService.getComRecruitersCount();//招聘人员数
            int enterpriseWaitCount = statisticsService.getModelCount(2);//待审核企业数量
            int publishWaitCount = statisticsService.countWait();//未审核岗位数量
            int nosUserCount = statisticsService.getNosUserCount();//未审核招聘人员数

            vo.setEnterpriseCount(enterpriseCount);
            vo.setJobCount(jobCount);
            vo.setWorkAppCount(workAppCount);
            vo.setBrowseCount(browseCount);
            vo.setJobFairCount(jobFairCount);
            vo.setCustomerCount(customerCount);
            vo.setNewsCount(newsCount);
            vo.setPublishWaitCount(publishWaitCount);
            vo.setComRecruitersCount(comRecruitersCount);
            vo.setEnterpriseWaitCount(enterpriseWaitCount);
            vo.setNosUserCount(nosUserCount);
        }
        return Result.success(vo);
    }

    //近七天岗位发布数
    @GetMapping("/getJobPublishCensus")
    public Result<Object> getJobPublishCensus(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = statisticsService.getCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }

    //近七天投递简历数
    @GetMapping("/getWorkApplyByTime")
    public Result<Object> getWorkApplyByTime(
            HttpServletRequest request) {

        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = statisticsService.getApplyCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);

        return Result.success(vo);
    }

    //近七天访问数
    @GetMapping("/getSevenSaysBrowse")
    public Result<Object> getSevenSaysBrowse(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = statisticsService.getBrowseCountByTime(time);
            xData.add(time);
            yData.add(count);

        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }

    //近七天新增用户数
    @GetMapping("/getSevenSaysCustomer")
    public Result<Object> getSevenSaysCustomer(
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }

        List<String> xData = new ArrayList<String>();
        List<Integer> yData = new ArrayList<Integer>();

        for (int i = 0; i < 7; i++) {
            String time = DateUtil.formatDate(DateUtil.addDays(new Date(), -i));
            int count = statisticsService.getCustomerCountByTime(time, null, null, 2);
            xData.add(time);
            yData.add(count);
        }
        IndexCountVo vo = new IndexCountVo();
        vo.setXData(xData);
        vo.setYData(yData);
        return Result.success(vo);
    }
}

